package controllers.cms.pages;

import models.cms.BlockDto;
import models.cms.PageDto;
import models.cms.TemplateDto;
import play.api.templates.Html;
import controllers.cms.CmsException;
import controllers.cms.blocks.BlockManager;

public abstract class AbstractPageManager implements PageManager {

	protected static Html renderBlock(PageDto page, BlockDto block) throws CmsException{
	  	TemplateDto t = block.getTemplate();
		String blockManagerName = "controllers.cms.blocks.BlockManagerImpl";
		if(t.getController() != null){
			blockManagerName = "controllers.cms.blocks." + t.getController();
		}
		try {
			Class blockManagerClass = Class.forName(blockManagerName);
			BlockManager blockManager = (BlockManager)blockManagerClass.newInstance();
			return blockManager.render(page,block);
		} catch (ClassNotFoundException e) {
			throw new CmsException(e.getMessage(),e);
		} catch (InstantiationException e) {
			throw new CmsException(e.getMessage(),e);
		} catch (IllegalAccessException e) {
			throw new CmsException(e.getMessage(),e);
		}  
  	}

}
